package com.craftplay.service;

import com.qcloud.Module.Sts;
import com.qcloud.QcloudApiModuleCenter;
import com.qcloud.Utilities.Json.JSONObject;
import com.qcloud.vod.VodUploadClient;
import com.qcloud.vod.model.VodUploadRequest;
import com.qcloud.vod.model.VodUploadResponse;
import it.sauronsoftware.jave.AudioUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.TreeMap;


@Service
public class TxCloudService {


    @Autowired
    private Environment env;


    private static final Logger logger = LoggerFactory.getLogger(TxCloudService.class);

    private static String SecretId = "AKIDVwSwkSmnvxobN6womT9au8KUVVSNo7mw";
    private static String SecretKey = "CHdN6pV5d4Pkvy2GdvHf2MccLzXzHTZa";
    private static String URL = "https://sts.api.qcloud.com/v2/index.php";

    public String GetFederationToken() {
        /* 如果是循环调用下面举例的接口，需要从此处开始您的循环语句。切记！ */
        TreeMap<String, Object> config = new TreeMap<String, Object>();
        config.put("SecretId", SecretId);
        config.put("SecretKey", SecretKey);

        /* 请求方法类型 POST、GET */
        config.put("RequestMethod", "GET");

        /* 区域参数，可选: gz: 广州; sh: 上海; hk: 香港; ca: 北美; 等。 */
        config.put("DefaultRegion", "gz");

        QcloudApiModuleCenter module = new QcloudApiModuleCenter(new Sts(),
                config);

        TreeMap<String, Object> params = new TreeMap<String, Object>();
        /* 将需要输入的参数都放入 params 里面，必选参数是必填的。 */
        /* DescribeInstances 接口的部分可选参数如下 */
        params.put("name", "sevenyou");
        String policy = " {" +
                "    \"version\": \"2.0\"," +
                "    \"statement\":" +
                "     {" +
                "         \"effect\": \"allow\"," +
                "         \"action\": \"cos:*\"," +
                "         \"resource\": \"*\"" +
                "     }" +
                "}";
        params.put("policy", policy);


        /* generateUrl 方法生成请求串, 可用于调试使用 */
        System.out.println(module.generateUrl("GetFederationToken", params));
        String result = null;
        try {
            /* call 方法正式向指定的接口名发送请求，并把请求参数 params 传入，返回即是接口的请求结果。 */
            result = module.call("GetFederationToken", params);
            JSONObject json_result = new JSONObject(result);
            return json_result.toString();
        } catch (Exception e) {
            System.out.println("error..." + e.getMessage());
        }
        return "";
    }

    /**
     * 上传录音文件等
     *
     * @param storeLocation
     */
    public VodUploadResponse VodUploadClient(String storeLocation) {
        VodUploadClient client = new VodUploadClient(SecretId, SecretKey);
        VodUploadRequest request = new VodUploadRequest();
        storeLocation = amrToMp3(storeLocation);
        request.setMediaFilePath(storeLocation);
        try {
            VodUploadResponse response = client.upload("ap-guangzhou", request);
            return response;
        } catch (Exception e) {
            // 业务方进行异常处理
            logger.error("Upload Err", e);
        }
        return null;
    }

    public String amrToMp3(String storeLocation) {
        try {
            File source = new File(storeLocation);
            String[] split = storeLocation.split("\\.");
            String s = split[0] + ".mp3";
            File target = new File(s);
            AudioUtils.amrToMp3(source, target);
            return s;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


}

